
#include "GToolBar.h"
#include "GWindowMgr.h"

#define POSX 200
#define POSY 400
#define DOCKPOSY 648

cairo_surface_t *image = NULL; 

gboolean onTBLeave_notify_event(GtkWidget	     *widget,
								GdkEventCrossing    *event);


static gboolean on_window_expose_event(GtkWidget * widget,
									   GdkEventExpose * event, gpointer data)
{
	cairo_t *cr;

	cr = gdk_cairo_create(widget->window);
	cairo_set_source_surface(cr, image, 0, 0);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_paint(cr);
	cairo_destroy(cr);
	return FALSE;
}


void CGToolBar::show()
{
	hide();
	gtk_widget_show( window );

}

void CGToolBar::hide()
{
	gtk_widget_hide( window );
}

void CGToolBar::create( const char* bg_file  )
{
	if ( NULL == bg_file )
		return;
	regWindow();

#ifdef DEMO3
	image = cairo_image_surface_create_from_png(bg_file);
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
	gtk_widget_set_app_paintable(window, TRUE);
	gtk_window_resize(GTK_WINDOW(window), 250, 250);
	g_signal_connect(G_OBJECT(window), "expose-event",
		G_CALLBACK(on_window_expose_event), NULL);

	screen = gtk_widget_get_screen(window);
	colormap = gdk_screen_get_rgba_colormap(screen);
	gtk_widget_set_colormap(window, colormap);
#else
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
	gtk_widget_set_app_paintable(window, TRUE);
	gtk_widget_realize(window);

	GdkPixbuf *pixbuf1;
	GdkBitmap *bitmap1;
	GdkPixmap *pixmap1;


	pixbuf = gdk_pixbuf_new_from_file(bg_file, NULL);
	pixbuf1 = gdk_pixbuf_new_from_file("resource\\texture\\link_over.png", NULL);

	gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
	gdk_pixbuf_render_pixmap_and_mask(pixbuf1, &pixmap1, &bitmap1, 128);

	gtk_widget_shape_combine_mask(window, bitmap, 0, 0);
	//gtk_widget_shape_combine_mask(window, bitmap1, 175, 0);

	gdk_window_set_back_pixmap(window->window, pixmap, FALSE);

#endif


	//gtk_window_set_opacity(GTK_WINDOW(window), 0.7);

	m_pFixed = gtk_fixed_new ();
	gtk_widget_show (m_pFixed);
	gtk_container_add (GTK_CONTAINER (window), m_pFixed);


	g_object_unref(pixbuf);
	g_object_unref(bitmap);
	g_object_unref(pixmap);

	gtk_widget_show_all(window);

	
	setAlpha( 0.7f );
	m_wndMediaDock.create( "resource/texture/suolueMdeia.png");
	m_wndMediaDock.hide();
	setPosition( POSX, POSY);
	setSize( 1024, 40 );
	_initButtons();

	g_signal_connect (G_OBJECT (window), "leave_notify_event",
		G_CALLBACK (onTBLeave_notify_event), NULL);


}

void CGToolBar::setPosition( int x, int y)
{
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_NONE);
	gtk_widget_set_uposition( window, x, y);
}

void CGToolBar::setSize( int x, int y)
{
	gtk_widget_set_size_request (window, x, y);

}

void CGToolBar::_initButtons()
{
	_initButton( m_btLink, "link", 175, 0);
	_initButton( m_btInternet, "inter", 293, 0);
	_initButton( m_btMedia, "media", 420, 0);
	_initButton( m_btDocument, "document", 550, 0);
	_initButton( m_btInterTest, "inter test", 667, 0);

	_initButton( m_btDos, "dos", 787, 0);

	m_btLink.G_MSG_CLICK.connect(this, &CGToolBar::onLinkClick);
	m_btLink.G_MSG_HOVER.connect(this, &CGToolBar::onLinkHover);

	m_btInternet.G_MSG_CLICK.connect(this, &CGToolBar::onInternetClick);
	m_btInternet.G_MSG_HOVER.connect(this, &CGToolBar::onInternetHover);
	m_btMedia.G_MSG_CLICK.connect(this, &CGToolBar::onMediaClick);
	m_btMedia.G_MSG_HOVER.connect(this, &CGToolBar::onMediaHover);
	m_btMedia.G_MSG_LEAVE.connect(this, &CGToolBar::onMediaLeave);


	m_btDocument.G_MSG_CLICK.connect(this, &CGToolBar::onDocumentClick);
	m_btDocument.G_MSG_HOVER.connect(this, &CGToolBar::onDocumentHover);

	m_btInterTest.G_MSG_CLICK.connect(this, &CGToolBar::onInterTestClick);
	m_btInterTest.G_MSG_HOVER.connect(this, &CGToolBar::onInterTestHover);
	
	m_btDos.G_MSG_CLICK.connect(this, &CGToolBar::onDosClick);
	m_btDos.G_MSG_HOVER.connect(this, &CGToolBar::onDosHover);

}

void CGToolBar::_initButton( CGButton& bt, const char* file_name, int posX, int posY)
{
	if ( NULL == file_name )
		return;
	char normal_buf[128] = {0};
	char hover_buf[128] = {0};
	char press_buf[128] = {0};
	char disable_buf[128] = {0};
	sprintf( normal_buf, "%s_normal.png", file_name);
	sprintf( hover_buf, "%s_over.png", file_name);
	sprintf( press_buf, "%s_down.png", file_name);
	sprintf( disable_buf, "%s_normal.png", file_name);

	bt.create();
	bt.createNormalImg(normal_buf);
	bt.createHoverImg(hover_buf);
	bt.createPressImg(press_buf);
	bt.createDisableImg(disable_buf);
	bt.setFixed( m_pFixed );

	bt.setPosition( posX, posY);
	bt.setSize( 64, 64);
	bt.show();

}


void CGToolBar::onDocumentClick()
{
	printf("click document\n");
}

void CGToolBar::onDocumentHover()
{
	printf("hover document\n");
}

void CGToolBar::onInternetClick()
{
	printf("click Internet\n");
}

void CGToolBar::onInternetHover()
{
	printf("hover document\n");
}

void CGToolBar::onInterTestClick()
{
	
}

void CGToolBar::onInterTestHover()
{
	
}

void CGToolBar::onLinkClick()
{
	
}

void CGToolBar::onLinkHover()
{
	
}

void CGToolBar::onMediaClick()
{
	
}

void CGToolBar::onMediaHover()
{
	m_wndMediaDock.setPosition(256+POSX, DOCKPOSY-POSY);
	m_wndMediaDock.setSize(401, 149);
	m_wndMediaDock.showEX();
}

void CGToolBar::onMediaLeave()
{
	m_wndMediaDock.hideEX();
}


void CGToolBar::onDosClick()
{

}

void CGToolBar::onDosHover()
{

}

void CGToolBar::_leaveButton(CGButton& bt)
{
	if ( bt.m_eState == E_BS_Hover )
		bt.leave();
}

void CGToolBar::_onMouseLeave()
{
	_leaveButton( m_btLink);
	_leaveButton( m_btInternet);
	_leaveButton( m_btMedia);
	_leaveButton( m_btDocument);
	_leaveButton( m_btInterTest);

	_leaveButton( m_btDos);

}

gboolean onTBLeave_notify_event(GtkWidget	     *widget,
								GdkEventCrossing    *event)
{
	CGToolBar * pWnd = (CGToolBar *)CGWindowMgr::getSingletonPtr()->getWindow( widget );

	if( pWnd )
		pWnd->_onMouseLeave();

	return 1;
}
